﻿package org.kolonitsky.alexey.banner {

    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
   
    import org.kolonitsky.alexey.net.RSSService;

    /**
     * RSS Banner. Developer for TUT.BY
     * This banner show rss feed. Scroll all news items. This class load external rss-
     * file, pars them and create UI elements that scroll.
     * @author Alexey Kolonitsky
     * @version 1.0
     * @updated 04-sep-2009 19:48:01
     */
    public class RSSBanner extends TUTBanner {
   
        /** show news delay in miliseconds **/ 
        protected var showLineTime: Number = 1800;
        
        /** timer **/
        protected var showItemTimer: Timer;
        
        /** rss feed uri **/
        private var uri: String;
      
        /** rss banner **/
        public var rssServ: RSSService;
      
        /** count **/
        public var count: uint;
      
      
        /**
        * Constructor
        */ 
        public function RSSBanner (uri:String, showTime:Number) 
        {
            super();
            trace("INF: RSS Banner created v0.0.1");
            
            this.uri = uri;
            this.showLineTime = showTime;
            
            rssServ = new RSSService();
            rssServ.addEventListener(Event.COMPLETE, onLoadRss);
            rssServ.load(this.uri);
        }
      
      
		/**
		 * Load rss feed handler
		 */ 
		protected function onLoadRss (event:Event): void 
        {
			trace("INF: RSSBanner.onLoadRss count=" + rssServ.length);
			count = rssServ.length > 47 ? 47 : rssServ.length;
			dispatchEvent(new Event(Event.COMPLETE));
		}


		/**
		 * 
		 */ 
		public function rssPlay (): void 
        {
			trace("INF: RSSBanner.rssPlay()")
			if (showItemTimer) 
				showItemTimer.start();
			else createTimer()
				dispatchEvent(new Event(Event.CHANGE));
		}


		/**
		 *
		 */ 
		public function rssPause (): void 
        {
			if (showItemTimer) 
                showItemTimer.stop();  
		}
		
		/**
		 *
		 */
		public function rssRestart (): void 
        {
			if (showItemTimer) 
            {
				showItemTimer.stop() 
				showItemTimer = null
			}
			createTimer()
		}


		/**
		 * Get index that now is current 
		 */      
		public function get currentIndex (): int 
        {
			return showItemTimer ? showItemTimer.currentCount : -1 
		}


		/**  **/
		protected function createTimer (): void 
        {
			showItemTimer = new Timer(showLineTime, count+1)
			showItemTimer.addEventListener(TimerEvent.TIMER, onChangeItem)
			showItemTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onShowLastItem)
			showItemTimer.start()
		}


		/**  **/
		protected function onChangeItem (event:TimerEvent): void 
        {
			trace("INF: RSSBanner.onChangeItem ")
			dispatchEvent(new Event(Event.CHANGE))
		}


		/**  **/
		protected function onShowLastItem (event:TimerEvent): void 
        {
			trace("INF: RSSBanner: Complete show feed")
		}
    }
}